package com.cspg.ssss.util;

import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class DateUtil {

    /*
计算两个日期之间的工作日（此处工作日为非周末，不计算节日）
 */
    public static int workDaysPeriod(Date strStartDate, Date strEndDate) {

        Calendar cl1 = Calendar.getInstance();
        Calendar cl2 = Calendar.getInstance();

        cl1.setTime(strStartDate);
        cl2.setTime(strEndDate);

        int count = 0;
        while (cl1.compareTo(cl2) <= 0) {
            if (cl1.get(Calendar.DAY_OF_WEEK) != 7 && cl1.get(Calendar.DAY_OF_WEEK) != 1)
                count++;
            cl1.add(Calendar.DAY_OF_MONTH, 1);
        }
        return count;

    }

    /*
    计算两个日期之间的工作日（此处工作日为非周末，不计算节日）
     */
    public static int workDays(String strStartDate, String strEndDate) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

        Calendar cl1 = Calendar.getInstance();
        Calendar cl2 = Calendar.getInstance();

        try {
            cl1.setTime(df.parse(strStartDate));
            cl2.setTime(df.parse(strEndDate));

        } catch (ParseException e) {
            e.printStackTrace();
        }

        int count = 0;
        while (cl1.compareTo(cl2) <= 0) {
            if (cl1.get(Calendar.DAY_OF_WEEK) != 7 && cl1.get(Calendar.DAY_OF_WEEK) != 1)
                count++;
            cl1.add(Calendar.DAY_OF_MONTH, 1);
        }
        return count;

    }

    public static String formatCalendar(Calendar cal) {
        Date now = cal.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String strDate = format.format(now);

        return strDate;
    }

    public static Date getWorkDay(String date) {
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
        Calendar cl1 = Calendar.getInstance();

        try {
            cl1.setTime(df.parse(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }

        boolean bBreak = false;
        while (!bBreak) {
            if (cl1.get(Calendar.DAY_OF_WEEK) != 1 && cl1.get(Calendar.DAY_OF_WEEK) != 7) {
                bBreak = true;
            } else {
                cl1.add(Calendar.DAY_OF_MONTH, 1);
            }
        }
        return cl1.getTime();
    }

    public static Date getCurrentDate() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    public static String getCurrentDateStr() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(calendar.getTime());
    }

    public static Date dayAddNum(Date time, Integer num) {
        //SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //Date date = format.parse(time);

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        calendar.add(Calendar.DAY_OF_MONTH, num);
        Date newTime = calendar.getTime();
        return newTime;
    }

    public static String toDateString(Date time) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日");
        return format.format(time);
    }

    public static void testDateMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, 2020);
        calendar.set(Calendar.MONTH, 7);
        calendar.set(Calendar.DAY_OF_MONTH, 15);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        calendar.add(Calendar.MONTH, -8);
        Date dt = calendar.getTime();
        String str = toDateString(dt);

    }

    public static void main(String[] args) throws UnsupportedEncodingException {

//        testDateMonth();

        String d1 = "2020-07-24";
        String d2 = "2020-07-20";
        int x = workDays(d1, d2);
        System.out.println(x);
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
//        String year = sdf.format(new Date());
//        System.out.println(year);
//
//        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
//        Date cl1 = new Date();
//        Date cl2 = new Date();
//
//        try {
//            cl1=df.parse("20200601");
//            cl2=df.parse("20200531");
//            Date wd = getWorkDay("20200531");
//            int r = cl1.compareTo(wd);
//            System.out.println(r);
//
//        } catch (ParseException e) {
//            e.printStackTrace();
//        }
//        long a1 = cl1.getTime();
//        long a2 = cl2.getTime();
//        long t = (a2 - a1)/1000;
//        long d = t/(24*60*60);
//        System.out.println(d);
//
//        Date startDate = DateUtil.getWorkDay("20200604");
//        Date nn = DateUtil.getCurrentDate();
//        int r = nn.compareTo(startDate);
    }
}
